<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    // 原始代码
    function calculate(operation, a, b) {
        if (operation === 'add') {
            return a + b;
        } else if (operation === 'subtract') {
            return a - b;
        } else if (operation === 'multiply') {
            return a * b;
        } else if (operation === 'divide') {
            return a / b;
        }
    }

    // 优化后
    const operations = {
        add: (a, b) => a + b,
        subtract: (a, b) => a - b,
        multiply: (a, b) => a * b,
        divide: (a, b) => a / b
    };

    function calculate(operation, a, b) {
        return operations[operation]?.(a, b) ?? 'Invalid operation';
    }

</script>
</html>